﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Ricciolo.PaperBoy.Feeds;
using Project_CFT2.Behavior;
using System.Windows.Input;

namespace Project_CFT2.ViewModel
{
    internal class FolderViewModel : ViewModelBase
    {
        private readonly FeedFolder _folder;
        //private static DataClasses1DataContext _dataDC = new DataClasses1DataContext();
        //private ObservableFurnitureItems _FurnitureItems;
        private static FurnitureDataClassesDataContext _dataDC = new FurnitureDataClassesDataContext();
        public static Obs_Furn_Items _furnitureItems;

        public FolderViewModel()
        {
            
            _furnitureItems = new Obs_Furn_Items(_dataDC);
           // _FurnitureItems = new ObservableFurnitureItems(_dataDC);
        }
       
        //public FolderViewModel(Furniture i)
        //{
            
        //    _FurnitureItems = new ObservableFurnitureItems(_dataDC);
        //}

        protected override string GetTitle() { return "Inventory"; }

        public bool IsRoot
        {
            get { return true; }
        }

        private ReadOnlyCollection<ViewModelBase> _items;

        public ReadOnlyCollection<ViewModelBase> Items
        {
            get
            {
                if (_items == null)
                {
                    var bw = new BackgroundWorker();
                    bw.DoWork += RefreshFoldersAndFeeds;
                    bw.RunWorkerAsync();
                }
                
                return _items;
            }
        }

       /* public ObservableFurnitureItems Items
        {
            get
            {


                return _furnitureItems;
            }
        }*/

        private DelegateCommand filterAllCommand;

        public ICommand FilterAllCommand
        {
            get
            {
                if (filterAllCommand == null)
                {
                    filterAllCommand = new DelegateCommand(FilterAll, CanFilter);
                }
                return filterAllCommand;
            }
        }

        private void FilterAll()
        {
            Filter("All", 1);
        }

        private DelegateCommand filterSofas1Command;

        public ICommand FilterSofas1Command
        {
            get
            {
                if (filterSofas1Command == null)
                {
                    filterSofas1Command = new DelegateCommand(FilterSofas1, CanFilter);
                }
                return filterSofas1Command;
            }
        }

        private void FilterSofas1()
        {
            Filter("Sofa", 1);
        }

        private DelegateCommand filterSofas2Command;

        public ICommand FilterSofas2Command
        {
            get
            {
                if (filterSofas2Command == null)
                {
                    filterSofas2Command = new DelegateCommand(FilterSofas2, CanFilter);
                }
                return filterSofas2Command;
            }
        }

        private void FilterSofas2()
        {
            Filter("Sofa", 2);
        }

        private DelegateCommand filterSofas3Command;

        public ICommand FilterSofas3Command
        {
            get
            {
                if (filterSofas3Command == null)
                {
                    filterSofas3Command = new DelegateCommand(FilterSofas3, CanFilter);
                }
                return filterSofas3Command;
            }
        }

        private void FilterSofas3()
        {
            Filter("Sofa", 3);
        }

        private DelegateCommand filterBedsCommand;

        public ICommand FilterBedsCommand
        {
            get
            {
                if (filterBedsCommand == null)
                {
                    filterBedsCommand = new DelegateCommand(FilterBeds, CanFilter);
                }
                return filterBedsCommand;
            }
        }

        private void FilterBeds()
        {
            Filter("Bed", 1);
        }

        private DelegateCommand filterChairsCommand;

        public ICommand FilterChairsCommand
        {
            get
            {
                if (filterChairsCommand == null)
                {
                    filterChairsCommand = new DelegateCommand(FilterChairs, CanFilter);
                }
                return filterChairsCommand;
            }
        }

        private void FilterChairs()
        {
            Filter("Chair", 1);
        }


        private DelegateCommand filterDesksCommand;

        public ICommand FilterDesksCommand
        {
            get
            {
                if (filterDesksCommand == null)
                {
                    filterDesksCommand = new DelegateCommand(FilterDesks, CanFilter);
                }
                return filterDesksCommand;
            }
        }

        private void FilterDesks()
        {
            Filter("Desk", 1);
        }


        private DelegateCommand filterTablesCommand;

        public ICommand FilterTablesCommand
        {
            get
            {
                if (filterTablesCommand == null)
                {
                    filterTablesCommand = new DelegateCommand(FilterTables, CanFilter);
                }
                return filterTablesCommand;
            }
        }

        private void FilterTables()
        {
            Filter("Table", 1);
        }
        private bool CanFilter()
        {
            return true;
        }

        private void Filter(string filterFor, int subType)
        {
            var foldersAndFeedsList = new List<ViewModelBase>();

            foreach (var item1 in _furnitureItems)
            {
                if (filterFor == "All" ||( item1.type == filterFor && item1.sub_type == subType))
                {
                    
                    myFurnitureDataItem x1 = new myFurnitureDataItem(item1);
                    foldersAndFeedsList.Add(new FurnitureViewModel(x1));
                }
            }
            _items = new ReadOnlyCollection<ViewModelBase>(foldersAndFeedsList);
            RaisePropertyChanged("Items");
        }

        void RefreshFoldersAndFeeds(object sender, DoWorkEventArgs e)
        {
            //_folder.FoldersAndFeeds.Refresh();
            LoadFoldersAndFeeds();
        }

        private void LoadFoldersAndFeeds()
        {
            var foldersAndFeedsList = new List<ViewModelBase>();

            
            foreach (var item1 in _furnitureItems)
            {
                


                //foldersAndFeedsList.Add(new FurnitureViewModel(new myDataItem(item1.Name.ToString(), item1.Price.ToString(), item1.Type.ToString(), item1.Price.ToString(), item1.Image.ToString())));
                myFurnitureDataItem x1 = new myFurnitureDataItem(item1);
                foldersAndFeedsList.Add(new FurnitureViewModel(x1));
                //foldersAndFeedsList.Add(new FurnitureViewModel(new myFurnitureDataItem(item1)));
            }
            /*
            foreach (var feedBase in _folder.FoldersAndFeeds)
            {
                if (feedBase is FeedFolder)
                {
                    var subFolder = feedBase as FeedFolder;
                    if (subFolder.TotalItemCount > 0)
                        foldersAndFeedsList.Add(new FolderViewModel(subFolder));
                }
                else if (feedBase is Feed)
                {
                    var feed = feedBase as Feed;
                    if (feed.ItemCount > 0)
                        foldersAndFeedsList.Add(new FurnitureViewModel(feed));
                }
            }*/
            
            _items = new ReadOnlyCollection<ViewModelBase>(foldersAndFeedsList);
            RaisePropertyChanged("Items");
            
        }
    }
}